package com.qfedu.product.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qfedu.product.po.Product;
import com.qfedu.product.service.productService;
import com.qfedu.product.service.impl.productServiceimpl;
import com.qfedu.user.service.userService;
import com.qfedu.user.service.impl.userServiceimpl;
import com.sun.org.apache.xpath.internal.operations.Bool;

import net.sf.json.JSON;
import net.sf.json.JSONObject;


@WebServlet("/productServlet")
public class productServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public productServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		response.setContentType("text/javascript");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		// 0.初始化组件
		PrintWriter writer = response.getWriter();
		// 1.接受数据
		String method = request.getParameter("method");

		productService productService = new productServiceimpl();
		List<Product> list = null;
		switch (method) {
		// 查询单天
		case "selectSellOne":
			String date1 = request.getParameter("operationTime1");
			String date2 = request.getParameter("operationTime2");
			System.out.println(date1);
			list = productService.selectSellOne(new Object[] {date1,date2});
			request.setAttribute("list", list);
			request.getRequestDispatcher("Sell_list.jsp").forward(request, response);

			break;
		/*
		 * case "selectSellAll": list = productService.selectSellAll();
		 * request.setAttribute("list", list); // 4.转发
		 * request.getRequestDispatcher("Sell_list.jsp").forward(request, response);
		 * 
		 * break;
		 */

		case "enter":
			String a = request.getParameter("id");
			String b = request.getParameter("num");
			String c = request.getParameter("name");
			String d = request.getParameter("cost");
			String e = request.getParameter("type");
			Object[]objects1=new Object[] {a,Integer.parseInt(b),c,Integer.parseInt(d),e};
           productServiceimpl productServiceimpl=new productServiceimpl();
		      boolean flag=	productServiceimpl.doEnter(objects1);
		      
		      writer.print(flag);	
		      writer.flush();
			
			
 break;
 
		case "add":
		   String id=	request.getParameter("id");
		   System.out.println(id);
		  productServiceimpl productServiceimpl2=new productServiceimpl();
			Product product;
			try {
				product = productServiceimpl2.add(id);
			} catch (SQLException e1) {
				product=null;
				e1.printStackTrace();
			}
			System.out.println(product);
           writer.print(JSONObject.fromObject(product));
    
 break;
		default:
			break;
		case "outer":
			String j=request.getParameter("production_id");
			String g=request.getParameter("production_num");
		Object[]objects=productService.doOuter(new Object[] {j,g});
		  
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
